package com.newsmanagement.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.newsmanagement.common.ResultBase;
import com.newsmanagement.config.MyBatisUtil;
import com.newsmanagement.mapper.UserMapper;
import com.newsmanagement.model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Reader;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;

/**
 * 用户操作controller
 * @author xbr
 * @version 1.0
 * 2017.12.08
 */
@CrossOrigin
@RestController
@RequestMapping(value = "user")
public class UserController {

    /**
     * 添加add
     * @return ResultBase
     */
    @RequestMapping("add")
    public ResultBase add(){

        // 初始化 User 对象
        User user = new User();
        user.setUserName("wangfeng");
        user.setUpdateTime(new Date(System.currentTimeMillis()));
        user.setCreateTime(new Date(System.currentTimeMillis()));
        user.setId(9);
        user.setAge(14);
        user.setSex(1);

        //完成数据库的插入
        user.insert();


        return new ResultBase("success", 200, user);
    }


    /**
     * 修改
     * @return ResultBase
     * http://localhost:8080/user/update?id=1&userName=xxx
     */
    @RequestMapping("update")
    public ResultBase update(
            @ModelAttribute(value = "user") User user
    ){
        user.updateById();

        return new ResultBase();
    }

    /**
     * 根据id查询对应的用户信息
     * @return ResultBase
     * http://localhost:8080/user/selectOne?id=1
     *
     */
    @RequestMapping("selectOne")
    public ResultBase selectOne(
            @RequestParam(value = "id") int id
    ){
        User user = new User();
        user = user.selectById(id);
        ResultBase resultBase = new ResultBase();
        resultBase.setData(user);
        return resultBase;
    }


    /**
     * 模糊查询姓名为‘wang’的所有用户记录
     * @param userName
     * @return ResultBase
     * http://localhost:8080/user/selectByName?userName=wang
     */
    @RequestMapping("selectByName")
    public ResultBase selectByName(
            @RequestParam(value = "userName") String userName
    ){

        User user = new User();

        List<User> userList1 = user.selectList(
                new EntityWrapper<User>().like("userName" ,"wang")
        );

        return new ResultBase("success", 200, userList1);
    }

    /**
     * 分页模糊查询姓名为‘wang’的所有用户记录
     * @param userName
     * @return ResultBase
     * http://localhost:8080/user/list?userName=wang
     */
    @RequestMapping("list")
    public ResultBase list(
            @RequestParam(value = "userName") String userName
    ){

        User user = new User();

        Object pageUser = user.selectPage(
                new Page<User>(2 ,2),
                new EntityWrapper<User>()
                        .like("userName" ,userName)
        );

        return new ResultBase("success", 200, pageUser);
    }


    /**
     * 删除
     * @return ResultBase
     * http://localhost:8080/user/delete?id=2
     */
    @RequestMapping("delete")
    public ResultBase delete(
            @RequestParam(value = "id") int id
    ){

        // 删除User

        User user = new User();

        boolean result = user.deleteById(id);
        if (!result){
            return new ResultBase("success", 200, result);
        }
        return new ResultBase();
    }


}
